package org.example.JUC.Tools;

import java.util.concurrent.*;

/**
 * @description:
 * @author: farprt
 * @create: 2022-09-10 18:09
 **/
public class CyclicBarrierTest2 {
    private static CyclicBarrier cyclicBarrier=new CyclicBarrier(2);

    public static void main(String[] args) {
        ExecutorService executorService= Executors.newFixedThreadPool(2);

        executorService.submit(new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println(Thread.currentThread()+"step1");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread()+"step2");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread()+"step3");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        });

        executorService.submit(new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println(Thread.currentThread()+"step1");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread()+"step2");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread()+"step3");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        });

        executorService.shutdown();
    }
}